package com.hitalkie.teacher.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.hitalkie.teacher.R;
import com.hitalkie.teacher.model.User;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


/**
 * Date 18/01/2017
 *
 * @author charliema
 */

public class LessonDetailStudentRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<User> mData = new ArrayList<>();

    private OnItemClickListener mOnItemClick;

    public void setData(List<User> list) {
        if (list != null) {
            this.mData = list;
        }
        notifyDataSetChanged();
    }

    public void addData(List<User> list) {
        if (list != null) {
            this.mData.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClick) {
        mOnItemClick = onItemClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder) holder).bindData(mData.get(holder.getAdapterPosition()));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * view holder
     */
    private class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivAvatar;
        TextView tvName;
        TextView tvStatus;

        ViewHolder(ViewGroup parent) {
            super(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lesson_detail_student, parent, false));

            ivAvatar = (ImageView) itemView.findViewById(R.id.ivAvatar);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            tvStatus = (TextView) itemView.findViewById(R.id.tvStatus);
        }

        void bindData(final User data) {
            if (data == null) {
                return;
            }
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClick != null) {
                        mOnItemClick.onItemClick(data);
                    }
                }
            });
            Picasso.with(ivAvatar.getContext()).load(data.avatar)
                    .placeholder(R.drawable.ic_default_avatar)
                    .error(R.drawable.ic_default_avatar)
                    .fit().into(ivAvatar);
            tvName.setText(data.name);


//            当type为null时按钮灰色不可点击,
            // 老师端：
//                    evaluate，
//            evaluated
            // 学生端：
//            apply_vip /
//                    cancel_vip /
//                    type=cancel_vip时按钮灰色可点击

            if (TextUtils.equals(data.operation.type, "evaluate")) {
                tvStatus.setVisibility(View.VISIBLE);
                tvStatus.setText(data.operation.title);
                tvStatus.setBackgroundResource(R.drawable.bg_frame_transparent_red_r4);
                tvStatus.setTextColor(tvStatus.getContext().getResources().getColor(R.color.colorPrimary));
                tvStatus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClick != null) {
                            mOnItemClick.onEvaluate(data);
                        }
                    }
                });
            } else {
                tvStatus.setOnClickListener(null);
                if (TextUtils.isEmpty(data.operation.title)) {
                    tvStatus.setVisibility(View.GONE);
                } else {
                    tvStatus.setVisibility(View.VISIBLE);
                    tvStatus.setText(data.operation.title);
                    tvStatus.setBackgroundResource(R.drawable.bg_frame_white_gray_r4);
                    tvStatus.setTextColor(tvStatus.getContext().getResources().getColor(R.color.black_99));
                }
            }


        }
    }

    /**
     * click event
     */
    public interface OnItemClickListener {
        void onItemClick(User data);

        void onEvaluate(User data);

    }

}
